package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.dto.QuoteDto;
import com.igeekhome.dto.QuoteRuleDto;
import com.igeekhome.mapper.*;
import com.igeekhome.pojo.*;
import com.igeekhome.service.IQuoteDtoService;
import com.igeekhome.util.CommonUtil;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Romeo
 */
@Service("quoteDtoService")
public class QuoteDtoServiceImpl implements IQuoteDtoService {

    @Autowired
    private QuoteDtoMapper quoteDtoMapper;
    @Autowired
    private DDeliveryToDestinationAreaQuotationMapper dDeliveryToDestinationAreaQuotationMapper;

    @Autowired
    private QuoteRuleMapper quoteRuleMapper;

    @Autowired
    private DSpecificQuotationSheetMapper dSpecificQuotationSheetMapper;

    @Autowired
    private DSendingMaintenanceListMapper dSendingMaintenanceListMapper;

    @Override
    public TableResultBean selectList(DDeliveryToDestinationAreaQuotation dDeliveryToDestinationAreaQuotation, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        dDeliveryToDestinationAreaQuotation.setStatus("1");
        List<QuoteDto> list = quoteDtoMapper.list(dDeliveryToDestinationAreaQuotation);
        PageInfo<QuoteDto> pageInfo = new PageInfo<>(list);
        if(list != null){
            return TableResultBean.ok(pageInfo.getTotal(),list);
        }
        return TableResultBean.fail("暂无数据");
    }

    @Override
    public ResultBean add(DDeliveryToDestinationAreaQuotation dDeliveryToDestinationAreaQuotation) {
        dDeliveryToDestinationAreaQuotation.setStatus("1");
        int insert = dDeliveryToDestinationAreaQuotationMapper.insertSelective(dDeliveryToDestinationAreaQuotation);
        if(insert > 0){
            return ResultBean.success();
        }
        return ResultBean.fail("新增失败");
    }

    @Override
    public ResultBean edit(DDeliveryToDestinationAreaQuotation dDeliveryToDestinationAreaQuotation) {
        int update = dDeliveryToDestinationAreaQuotationMapper.updateByPrimaryKeySelective(dDeliveryToDestinationAreaQuotation);
        if(update > 0) {
            return ResultBean.success();
        }
        return ResultBean.fail("更新失败");
    }

    @Override
    public ResultBean delete(String idsStr) {
        if(idsStr.contains(",")){
        idsStr = idsStr.substring(0,idsStr.length()-1);
    }
        if(!idsStr.contains(",")){
            // 单个删除
            DDeliveryToDestinationAreaQuotation quotation = new DDeliveryToDestinationAreaQuotation();
            quotation.setStatus("0");

            DDeliveryToDestinationAreaQuotationExample example = new DDeliveryToDestinationAreaQuotationExample();
            example.createCriteria().andOfferIdEqualTo(Integer.parseInt(idsStr));
            int delete = dDeliveryToDestinationAreaQuotationMapper.updateByExampleSelective(quotation, example);
            if(delete > 0){
                return ResultBean.success();
            }
        }else{
            // 批量删除
            int delete = dDeliveryToDestinationAreaQuotationMapper.deleteByIdStr(idsStr);
            if(delete > 0){
                return ResultBean.success();
            }
        }
        return ResultBean.fail("删除失败");

    }

    @Override
    public TableResultBean ruleList(Integer page,Integer limit,String itemId,String shiftId,String offerId) {
        PageHelper.startPage(page,limit);
        HashMap<String,String> map = new HashMap<String,String>(3){
            {
                put("itemId",itemId);
                put("shiftId",shiftId);
                put("offerId",offerId);
            }
        };
        List<QuoteRuleDto> quoteRuleDtos = quoteRuleMapper.selectByOthers(map);
        PageInfo<QuoteRuleDto> pageInfo = new PageInfo<>(quoteRuleDtos);
        if(quoteRuleDtos != null && quoteRuleDtos.size() > 0) {
            return TableResultBean.ok(pageInfo.getTotal(),quoteRuleDtos);
        }
        return TableResultBean.fail("暂无数据");
    }

    @Override
    public ResultBean ruleAdd(DSpecificQuotationSheet dSpecificQuotationSheet) {
        if(dSpecificQuotationSheet.getCustomerType() == null && dSpecificQuotationSheet.getCustomerId() == null){
            dSpecificQuotationSheet.setCustomerType(0).setStatus(1);
        } else {
            dSpecificQuotationSheet.setCustomerType(1).setStatus(1);
        }
        int insert  = dSpecificQuotationSheetMapper.insertSelective(dSpecificQuotationSheet);
        if(insert > 0){
            return ResultBean.success();
        }
        return ResultBean.fail("新增失败");
    }

    @Override
    public ResultBean ruleEdit(DSpecificQuotationSheet dSpecificQuotationSheet) {
        Integer customerId = dSpecificQuotationSheet.getCustomerId();
        if(customerId != null ) {
            dSpecificQuotationSheet.setCustomerType(0);
        } else {
            dSpecificQuotationSheet.setCustomerType(1);
        }
        int update = dSpecificQuotationSheetMapper.updateByPrimaryKeySelective(dSpecificQuotationSheet);
        if(update > 0){
            return ResultBean.success();
        }
        return ResultBean.fail("更新失败");
    }

    @Override
    public List<String[]> selectFormula(Integer serialId) {
        DSpecificQuotationSheet d = dSpecificQuotationSheetMapper.selectByPrimaryKey(serialId);
        String strFormula = d.getQuotationCalculationFormula();
        // 将公式拆成数组然后转换成list集合
        return formulaToList(strFormula);
    }

    @Override
    public ResultBean ruleDelete(Integer serialId) {
        DSpecificQuotationSheet ds = new DSpecificQuotationSheet().setStatus(0).setSerialId(serialId);
        int delete = dSpecificQuotationSheetMapper.updateByPrimaryKeySelective(ds);
        if(delete > 0) {
            return ResultBean.success();
        }
        return ResultBean.fail("删除失败");
    }

    @Override
    public List<String[]>  selectFormula(Integer sending,Integer destination, Integer shift, Integer itemType, Integer vip) {
        HashMap<String,Integer> map = new HashMap<String,Integer>(5){
            {
                put("sending",sending);
                put("destination",destination);
                put("shift",shift);
                put("itemType",itemType);
                put("vip",vip);
            }
        };
        List<String> formulas = quoteRuleMapper.selectFormula(map);
        System.out.println("formulas==="+formulas);
        if(formulas != null && formulas.size() > 0){
            return formulaToList(formulas.get(0));
        }
        return null;
    }

    @Override
    public List<DSendingMaintenanceList> selectByNid(Integer nid) {
        return quoteRuleMapper.selectByNid(nid);
    }

    @Override
    public List<DSendingMaintenanceList> selectToSpots() {
        DSendingMaintenanceListExample example = new DSendingMaintenanceListExample();
        example.createCriteria().andSectionTypeEqualTo(1).andStatusEqualTo(1);
        return dSendingMaintenanceListMapper.selectByExample(example);
    }

    /**
     *  拆解公式
     * @param strFormula String
     * @return List<String[]>
     */
    private List<String[]> formulaToList(String strFormula){
        if(strFormula == null && strFormula.length() == 0){
            return null;
        }
        List<String[]> formula = new ArrayList<>();
        String[] s = strFormula.split("_");
        for (String s1 : s) {
            String[] ss = s1.split(",");
            formula.add(ss);
        }
        return formula;
    }
}
